<?php

namespace app\admin\controller;

use think\facade\Session;
use app\admin\controller\Base;
use app\admin\model\AuthRule as RuleModel;

class AuthRule extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new RuleModel();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $list = $this->Model->allListMenu();
        $this->assign("list", $list);
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if($this->request->isPost()){
            $data = $this->getPostData();
            $res = $this->Model->createOrUpdate($data);
            if($res["status"]){
                Session::flash('success', $res['msg']);
                $this->success($res['msg'], 'index');
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res['msg']);
            }
        }else{
            $parents = $this->Model->allListMenu();
            $this->assign("parents", $parents);
            $this->assign('rule', ['status' => 1, 'parent_id' => 0]);
            return $this->fetch('form');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id = 0)
    {
        $id = intval($id);
        $rule = $this->Model->where('id', $id)->find();
        if(empty($rule)){
            Session::flash('danger', '未找到编辑记录');
            $this->redirect('index');
        }

        if($this->request->isPost()){
            $data = $this->getPostData();
            $res = $this->Model->createOrUpdate($data, $id);
            if($res['status']){
                Session::flash('success', $res['msg']);
                $this->success($res['msg'], 'index');
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res['msg']);
            }
        }else{
            $parents = $this->Model->allListMenu();
            $this->assign("parents", $parents);
            $this->assign('rule', $rule);
            return $this->fetch('form');
        }
    }

    private function getPostData()
    {
        $fields = [
            ['name' => 'controller', 'type' => 'string', 'default' => ''],
            ['name' => 'action', 'type' => 'string', 'default' => ''],
            ['name' => 'title', 'type' => 'string', 'default' => ''],
            ['name' => 'condition', 'type' => 'string', 'default' => ''],
            ['name' => 'icon', 'type' => 'string', 'default' => ''],
            ['name' => 'tips', 'type' => 'string', 'default' => ''],
            ['name' => 'sort', 'type' => 'integer', 'default' => 0],
            // 使用switch开关，所以默认值必须是false，因为switch为关闭状态时返回的值里没有该字段
            ['name' => 'status', 'type' => 'boolean', 'default' => false],
        ];

        $default = [];

        return $this->parsePostParam($fields, $default);
    }
}
